package com.ruoyi.quartz.task;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.ZftSubMerchantOrder;
import com.alipay.api.response.AntMerchantExpandIndirectZftorderQueryResponse;
import com.ruoyi.business.domain.TAliZftMerchantInfo;
import com.ruoyi.business.domain.TIsvInfo;
import com.ruoyi.business.domain.TPayChannel;
import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.service.*;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("alizftMerchantTask")
public class AlizftMerchantTask
{
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ITAliZftMerchantInfoService tAliZftMerchantInfoService;

    @Autowired
    private ITIsvInfoService itIsvInfoService;

    @Autowired
    private AliZftPayToolService aliZftPayToolService;

    @Resource
    private ITRecipientInfoService recipientInfoService;
    @Resource
    private ITPayChannelService payChannelService;
    public void checkAlizftMerchant() throws Exception {
        logger.info("检测支付宝直付通二级商户");
        TAliZftMerchantInfo query = new TAliZftMerchantInfo();
        query.setStatus(0);
        List<TAliZftMerchantInfo> tAliZftMerchantInfoList = tAliZftMerchantInfoService.selectTAliZftMerchantInfoListByNoAuth(query);
        logger.info("检测支付宝直付通二级商户记录数：{}",tAliZftMerchantInfoList.size());
        for(TAliZftMerchantInfo tAliZftMerchantInfo :tAliZftMerchantInfoList){
            AntMerchantExpandIndirectZftorderQueryResponse response = aliZftPayToolService.AntMerchantExpandIndirectZftorderQuery(tAliZftMerchantInfo);
            logger.info("检测支付宝直付通二级商户记录：{}",JSONObject.toJSONString(response));
            if(response.isSuccess()){
                TIsvInfo tIsvInfo = itIsvInfoService.selectTIsvInfoById(Long.valueOf(tAliZftMerchantInfo.getIsvId()));
//            AlipayClient alipayClient = getAliConfig(tIsvInfo);
                JSONObject body = JSONObject.parseObject(response.getBody());
                JSONObject ant_merchant_expand_indirect_zftorder_query_response = body.getJSONObject("ant_merchant_expand_indirect_zftorder_query_response");
                if(ant_merchant_expand_indirect_zftorder_query_response.getString("code").equals("10000")){
                    List<ZftSubMerchantOrder> zftSubMerchantOrderList  = response.getOrders();
                    for(ZftSubMerchantOrder zftSubMerchantOrder:zftSubMerchantOrderList){
                        if(zftSubMerchantOrder.getStatus().equals("99")) {
                            tAliZftMerchantInfo.setSmid(zftSubMerchantOrder.getSmid());
                            tAliZftMerchantInfo.setMessage(ant_merchant_expand_indirect_zftorder_query_response.toJSONString());
                            //审核通过
                            tAliZftMerchantInfo.setStatus(1);
                            tAliZftMerchantInfoService.updateTAliZftMerchantInfo(tAliZftMerchantInfo);
                            //审核通过添加到收款列表
                            TRecipientInfo tRecipientInfo = new TRecipientInfo();
                            tRecipientInfo.setChannelType("alipay");
                            tRecipientInfo.setCreateTime(DateUtils.getNowDate());
                            tRecipientInfo.setChannelAccount(tAliZftMerchantInfo.getSmid());
                            tRecipientInfo.setChannelDisabled(0);
                            tRecipientInfo.setAvailable(1);
                            tRecipientInfo.setOnline(1);
                            tRecipientInfo.setNeedOnline(1);
                            TPayChannel payChannelParam = new TPayChannel();
                            payChannelParam.setChannel("isv_alipay_zft");
                            TPayChannel orderChannel = payChannelService.selectTPayChannelList(payChannelParam).get(0);
                            tRecipientInfo.setChannel("isv_alipay_zft");
                            tRecipientInfo.setNeedOnline(orderChannel.getNeedOnLine());
                            tRecipientInfo.setLoginType(orderChannel.getLoginType());
                            if (ObjectUtil.isNotEmpty(tRecipientInfo.getPassWord())) {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("password", tRecipientInfo.getPassWord());
                                tRecipientInfo.setAccountJson(jsonObject.toJSONString());
                            }
                            if (ObjectUtil.isNotEmpty(tRecipientInfo.getYlKey())) {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("ylKey", tRecipientInfo.getYlKey());
                                tRecipientInfo.setAccountJson(jsonObject.toJSONString());
                            }
                            if (ObjectUtil.isEmpty(tRecipientInfo.getShopNickname())) {
                                tRecipientInfo.setShopNickname(null);
                            }
                            if (ObjectUtil.isEmpty(tRecipientInfo.getShopClerkAccount())) {
                                tRecipientInfo.setShopClerkAccount(null);
                            }
                            if (tRecipientInfo.getChannel().equals("card_to_card")) {
                                if (tRecipientInfo.getBankName().contains("邮储")) {
                                    tRecipientInfo.setWeiShu(tRecipientInfo.getCardNo().substring(tRecipientInfo.getCardNo().length() - 3, tRecipientInfo.getCardNo().length()));
                                } else {
                                    tRecipientInfo.setWeiShu(tRecipientInfo.getCardNo().substring(tRecipientInfo.getCardNo().length() - 4, tRecipientInfo.getCardNo().length()));
                                }
                            }
                            if (tRecipientInfo.getChannel().startsWith("isv_alipay")) {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("aliPublicPath", tIsvInfo.getIsvAliPublicPath());
                                jsonObject.put("aliPrivateKey", tIsvInfo.getIsvAliPrivateKey());
                                jsonObject.put("appPublicPath", tIsvInfo.getIsvAppPublicPath());
                                jsonObject.put("aliRootPath", tIsvInfo.getIsvAliRootPath());
                                tRecipientInfo.setAccountJson(jsonObject.toJSONString());
                            }
                            tRecipientInfo.setAlipayCardid(tIsvInfo.getId().toString());
                            tRecipientInfo.setUserId(tAliZftMerchantInfo.getUserId());
                            recipientInfoService.insertTRecipientInfo(tRecipientInfo);
                        }
                    }
                }else {
                    tAliZftMerchantInfo.setMessage(response.getMsg());
                    tAliZftMerchantInfo.setStatus(2);
                    tAliZftMerchantInfoService.updateTAliZftMerchantInfo(tAliZftMerchantInfo);
                }
            } else {
                logger.info("调用失败");
                tAliZftMerchantInfo.setMessage(response.getMsg());
                tAliZftMerchantInfo.setStatus(2);
                tAliZftMerchantInfoService.updateTAliZftMerchantInfo(tAliZftMerchantInfo);
            }
        }
    }

}
